﻿using System;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace ProcessesSMS
{
    /// <summary>
    /// Summary description for MUAVN.
    /// </summary>
    public class QT
    {
        #region Declare
        private SMS_MO WMO;
        private SMS_MT WMT;
        //ArrayList arr = new ArrayList();
        public QT(SMS_MO MO)
        {
            WMO = MO;
        }

        public QT()
        {
        }
        #endregion
        #region Method
        public void run()
        {
            WMT = new SMS_MT(WMO);
            //string tmpMsg = WMO.Message.Substring(WMO.CommandCode.Length).Trim().Replace("  ", " ").Replace("  ", " ");
            //tmpMsg = tmpMsg.Replace("  ", " ").Trim().ToUpper();
            //string[] msg = tmpMsg.Split(new char[] { ' ' });
            string[] strReturnMsg = GetReturnMsg().Split('~');
            string serviceID=WMO.ServiceID.Replace("04","");
            if (serviceID == "6572" || serviceID == "6672" || serviceID == "6772")
            {
                for (int i = 0; i < strReturnMsg.Length-1; i++)
                {
                    if (!string.IsNullOrEmpty(strReturnMsg[i].Trim()))
                    {
                        WMT.AddMessage(WMO.UserID, strReturnMsg[i].Trim(), SMS_ContentType.SMS_TEXT);
                        WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
                    }
                }
            }
            else
            {
                WMT.AddMessage(WMO.UserID, strReturnMsg[strReturnMsg.Length-1].Trim(), SMS_ContentType.SMS_TEXT);
                WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
            }
            WMT.Insert();
            return;
        }
        private string GetReturnMsg()
        {
            string msgBody = "";
            string connStr = ConfigurationSettings.AppSettings["CONNECTION_STRING_7x62"].ToString();
            string strSQL = "select mt from QT_3M where systax='QT'";
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter(strSQL, conn);
            DataSet ds = new DataSet();
            da.Fill(ds,"QT");
            conn.Close();
            msgBody = ds.Tables["QT"].Rows[0]["mt"].ToString().Trim();
            ds.Dispose();
            return msgBody;
        }
        #endregion
    }
}
